package com.javasm.sys.controller;

import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.enums.E;
import com.javasm.commons.exceptions.MvcException;
import com.javasm.sys.MenuService;
import com.javasm.sys.SysMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    private static Logger l = LoggerFactory.getLogger(MenuController.class);


    @GetMapping("getMenuById")
    public AxiosResult getMenuById(Integer id){
        SysMenu menuByid = menuService.getMenuByid(id);
        if(menuByid!=null){
            return AxiosResult.suc(menuByid);
        }else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }
    @GetMapping("getallmenu")
    public AxiosResult getallmenu(){
        List<SysMenu> getallmenu = menuService.getallmenu();
        if(getallmenu!=null){
            return AxiosResult.suc(getallmenu);
        }else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }
    @GetMapping("getMenuByparams")
    public AxiosResult getMenuByparams(SysMenu sysMenu){
        List<SysMenu> menuByparams = menuService.getMenuByparams( sysMenu);

        if(menuByparams!=null){
            return AxiosResult.suc(menuByparams);
        }else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }

    @GetMapping("getmenuBytype")
    public AxiosResult getmenuBytype(Integer menuType){
        List<SysMenu> sysMenusBytype = menuService.getmenuBytype(menuType);
        if(sysMenusBytype!=null){
            return AxiosResult.suc(sysMenusBytype);
        }else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }

    @GetMapping("getLastIdByParentId")
    public AxiosResult getLastIdByParentId(Integer pid){
        Integer lastIdByParentId = menuService.getLastIdByParentId(pid);
        if(lastIdByParentId!=null){
            return AxiosResult.suc(lastIdByParentId);
        }else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }





    @PostMapping("insert")//菜单
    public AxiosResult insert(@RequestBody SysMenu sysMenu){
        Integer generateId = menuService.generateId(sysMenu.getPid());//生成id
        if(generateId==null) throw new MvcException(E.NOT_EXISTS);//判断id是否正确
        sysMenu.setId(generateId);//在控制层为id赋值

        int insertResult = menuService.insert(sysMenu);
        if(insertResult!=0){
            return AxiosResult.suc(E.SUC);
        }else {
            return AxiosResult.error(E.OPERATION_FAILED);
        }
    }

    @PostMapping("insert2")//目录
    public AxiosResult insert2(@RequestBody SysMenu sysMenu){
        Integer generateId = menuService.generateId(sysMenu.getPid());//生成id
        if(generateId==null) throw new MvcException(E.NOT_EXISTS);//判断id是否正确
        sysMenu.setId(generateId);//在控制层为id赋值
//        sysMenu.setPid(0);

        int insertResult = menuService.insert2(sysMenu);
        if(insertResult!=0){
            return AxiosResult.suc(E.SUC);
        }else {
            return AxiosResult.error(E.OPERATION_FAILED);
        }
    }


    @PutMapping
    public AxiosResult updateByid(@RequestBody SysMenu sysMenu){
        int updateResult = menuService.updateByid(sysMenu);
        if(updateResult!=0){
            return AxiosResult.suc(E.SUC);
        }else {
            return AxiosResult.error(E.OPERATION_FAILED);
        }
    }
    @DeleteMapping
    public AxiosResult delById(Integer id){
        int delResult = menuService.delById(id);
        if(delResult!=0){
            return AxiosResult.suc(E.SUC);
        }else {
            return AxiosResult.error(E.OPERATION_FAILED);
        }
    }











}
